#include "addbookwindow.h"

AddBookWindow::AddBookWindow(Database *db, QWidget *parent)
    : QDialog(parent)
    , db(db)
{
    setupUI();
    createConnections();
}

AddBookWindow::~AddBookWindow()
{
}

void AddBookWindow::setupUI()
{
    setWindowTitle("添加图书");
    setFixedSize(400, 300);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 图书ID
    QLabel *idLabel = new QLabel("图书ID:", this);
    idEdit = new QLineEdit(this);
    idEdit->setPlaceholderText("请输入图书ID");
    idEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(idLabel);
    mainLayout->addWidget(idEdit);

    // 书名
    QLabel *titleLabel = new QLabel("书名:", this);
    titleEdit = new QLineEdit(this);
    titleEdit->setPlaceholderText("请输入书名");
    titleEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(titleEdit);

    // 作者
    QLabel *authorLabel = new QLabel("作者:", this);
    authorEdit = new QLineEdit(this);
    authorEdit->setPlaceholderText("请输入作者");
    authorEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(authorLabel);
    mainLayout->addWidget(authorEdit);

    // 出版社
    QLabel *publisherLabel = new QLabel("出版社:", this);
    publisherEdit = new QLineEdit(this);
    publisherEdit->setPlaceholderText("请输入出版社");
    publisherEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(publisherLabel);
    mainLayout->addWidget(publisherEdit);

    // 可借阅状态
    availableCheckBox = new QCheckBox("可借阅", this);
    availableCheckBox->setChecked(true);
    mainLayout->addWidget(availableCheckBox);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    addButton = new QPushButton("添加", this);
    cancelButton = new QPushButton("取消", this);

    addButton->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; padding: 8px 16px; "
                           "border: none; border-radius: 4px; }"
                           "QPushButton:hover { background-color: #388E3C; }"
                           "QPushButton:pressed { background-color: #1B5E20; }");

    cancelButton->setStyleSheet("QPushButton { background-color: #F44336; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #D32F2F; }"
                              "QPushButton:pressed { background-color: #B71C1C; }");

    buttonLayout->addWidget(addButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QDialog { background-color: white; }");
}

void AddBookWindow::createConnections()
{
    connect(addButton, &QPushButton::clicked, this, &AddBookWindow::onAddClicked);
    connect(cancelButton, &QPushButton::clicked, this, &AddBookWindow::onCancelClicked);
}

void AddBookWindow::onAddClicked()
{
    QString id = idEdit->text().trimmed();
    QString title = titleEdit->text().trimmed();
    QString author = authorEdit->text().trimmed();
    QString publisher = publisherEdit->text().trimmed();
    bool available = availableCheckBox->isChecked();

    if (id.isEmpty() || title.isEmpty() || author.isEmpty() || publisher.isEmpty()) {
        QMessageBox::warning(this, "警告", "所有字段都不能为空！");
        return;
    }

    if (db->addBook(id, title, author, publisher, available)) {
        QMessageBox::information(this, "成功", "图书添加成功！");
        accept();
    } else {
        QMessageBox::warning(this, "错误", "图书添加失败！图书ID可能已存在。");
    }
}

void AddBookWindow::onCancelClicked()
{
    reject();
} 